package com.chanyachting.emall.serviceImpl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import com.chanyachting.emall.dao.CompanyDAO;
import com.chanyachting.emall.dao.RecruitmentinformationDAO;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Joblist;
import com.chanyachting.emall.domain.Recruitmentinformation;
import com.chanyachting.emall.service.RecruitmentinformationService;
import com.chanyachting.emall.util.DateUtil;

public class RecruitmentinformationServiceImpl implements
		RecruitmentinformationService {
	
	private RecruitmentinformationDAO riDao;

	public RecruitmentinformationServiceImpl() {
		
		this.riDao=new RecruitmentinformationDAO();
	}

	public boolean createRecruitmentinformation(Recruitmentinformation ri) {
		
		this.riDao.save(ri);
		
		return true;
	}

	public boolean delRecruitmentinformation(Recruitmentinformation ri) {
		
		this.riDao.delete(ri);
		
		return true;
	}

	public boolean displayRecruitmentinformation(Recruitmentinformation ri) {
		
		ri.setStatus("00");
		
		ri.setUpdateDate(DateUtil.getDate());
		
		this.riDao.update(ri);
		
		return true;
	}

	public List<Recruitmentinformation> getAllDisplayRecruitmentinformationByCompany(
			Company company) {
		
		String args[]={String.valueOf(company.getId())};
		
		return this.riDao.find("from Recruitmentinformation where companyId=? and status='00'", args);
	}

	public List<Recruitmentinformation> getAllPublishRecruitmentinformationByCompany(
			Company company) {
		
		String args[]={String.valueOf(company.getId())};
		
		return this.riDao.find("from Recruitmentinformation where companyId=? and status='01'", args);
	}

	public List<Recruitmentinformation> getAllRecruitmentinformation() {
		
		return this.riDao.find("from Recruitmentinformation");
	}

	public List<Recruitmentinformation> getAllRecruitmentinformationByCompany(
			Company company) {
		
		String args[]={String.valueOf(company.getId())};
		
		return this.riDao.find("from Recruitmentinformation where companyId=?", args);
	}

	public Recruitmentinformation getOneRecruitmentinformation(
			Recruitmentinformation ri) {
		
		return (Recruitmentinformation) this.riDao.load(Recruitmentinformation.class, ri.getId());
	}

	public boolean publishRecruitmentinformation(Recruitmentinformation ri) {
		
		ri.setStatus("01");
		
		ri.setUpdateDate(DateUtil.getDate());
		
		ri.setPublishDate(DateUtil.getDate());
		
		ri.setRefreshDate(DateUtil.getDate());
		
		this.riDao.update(ri);
		
		return true;
	}

	public boolean refreshRecruitmentinformation(Recruitmentinformation ri) {
		
		ri.setRefreshDate(DateUtil.getDate());
		
		this.riDao.update(ri);
		
		return true;
	}

	public boolean updateRecruitmentinformation(Recruitmentinformation ri) {
		
		this.riDao.update(ri);
		
		return true;
	}

	public boolean addJobList(Joblist job) {
		
		this.riDao.save(job);
		
		return true;
	}

	public boolean deleteJobList(Joblist job) {
	
		this.riDao.delete(job);
		
		return true;
	}

	public List getCompanyInformation() {
		List<Recruitmentinformation> result = this.riDao.getRecruitmentinformation();
		
		CompanyDAO cmDao = null;

		if (result == null) {
			return null;
		}
		
		for(int i=0;i < result.size(); i++) {
			cmDao = new CompanyDAO();
			
			//公司的性质取得
			String tmpCompanyType = cmDao.getCompany(result.get(i).getCompanyId().toString()).getCompanyType();
			
			result.get(i).setContactName(tmpCompanyType);
			
//			DateFormat tmpDate = new SimpleDateFormat("yyyy-MM");
//			String chgDate = tmpDate.format(result.get(i).getCreateDate());
			String chgDate = DateUtil.changeDateFormat(result.get(i).getCreateDate());
			
			System.out.println("============================");
			System.out.println(chgDate + "==========" + result.get(i).getCreateDate());
			System.out.println("============================");
			result.get(i).setCreateDate(chgDate);
			
			cmDao = null;
		}
		
		return result;
	}
	
	
	public Joblist getJobList(String rid,String jobId) {
		String sqlStr = "from Recruitmentinformation where id=?";
		List<Recruitmentinformation> tmpRi = this.riDao.find(sqlStr, new String[]{rid});
		Joblist j = null;
		
		if (tmpRi.size() == 0) {
			return null;
		}
		
		Set jobList = tmpRi.get(0).getJoblistSet();
		if (jobList.size() == 0) {
			return null;
		}
		
		Iterator jobs = jobList.iterator();
		
		System.out.println("======================");
		System.out.println("=======job=========" + jobs);
		
		while(jobs.hasNext()) {
			j = (Joblist)jobs.next();
			if (jobId.equals(j.getId().toString())) {
				break;
			}
			j = null;
		}
		return j;
	}
	
	@SuppressWarnings("unchecked")
	public List<Recruitmentinformation> getRecruitmentInfByPage(int currentPage,int pageSize) {
		String sql = "from Recruitmentinformation where status = '01' order by publishDate desc";
		Query q = riDao.createQuery(sql);
		
		q.setFirstResult((currentPage - 1) * pageSize);
		q.setMaxResults(pageSize);
		
		return q.list();
	}
	
	public int getRecruitmentInfCount() {
		String sql = "select count(*) from Recruitmentinformation where status = '01'";
		
		return (Integer)riDao.find(sql).get(0);
	}
}
